using B.S.Domain.AICustomerService;
using B.S.Infratrctrue;
using Microsoft.EntityFrameworkCore;
using Yitter.IdGenerator;

namespace B.S.Data.MES.API.SampleData
{
    /// <summary>
    /// AI客服示例数据
    /// </summary>
    public static class AICustomerServiceSampleData
    {
        /// <summary>
        /// 初始化AI客服示例数据
        /// </summary>
        public static async Task InitializeAsync(SysDbcontext context)
        {
            try
            {
                // 检查是否已经存在数据
                var existingKnowledge = await context.AIKnowledgeBases.AnyAsync();
                if (existingKnowledge)
                    return;

                // 创建知识库示例数据
                var knowledgeList = new List<AIKnowledgeBase>
                {
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "系统登录问题",
                        Question = "如何登录系统？登录不了怎么办？",
                        Answer = "请按以下步骤登录：\n1. 打开系统登录页面\n2. 输入用户名和密码\n3. 点击登录按钮\n\n如果登录失败，请检查：\n- 用户名和密码是否正确\n- 网络连接是否正常\n- 浏览器是否支持\n\n如问题仍然存在，请联系管理员。",
                        Category = "系统操作",
                        Tags = "登录,用户名,密码,系统",
                        Priority = 5,
                        IsEnabled = true,
                        MatchThreshold = 0.7m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "密码重置",
                        Question = "忘记密码怎么办？如何重置密码？",
                        Answer = "重置密码的步骤：\n1. 在登录页面点击\"忘记密码\"\n2. 输入您的用户名或邮箱\n3. 系统会发送重置链接到您的邮箱\n4. 点击邮箱中的链接设置新密码\n\n如果没有收到邮件，请：\n- 检查垃圾邮件文件夹\n- 确认邮箱地址正确\n- 联系管理员手动重置",
                        Category = "账户管理",
                        Tags = "密码,重置,忘记,邮箱",
                        Priority = 4,
                        IsEnabled = true,
                        MatchThreshold = 0.8m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "生产计划查询",
                        Question = "如何查询生产计划？在哪里查看生产进度？",
                        Answer = "查询生产计划的方法：\n1. 登录系统后进入\"生产管理\"模块\n2. 点击\"生产计划\"菜单\n3. 可以按日期、产品、订单号等条件筛选\n4. 查看计划详情和执行进度\n\n生产进度查看：\n- 甘特图显示：直观查看进度\n- 列表显示：详细数据查看\n- 导出功能：可导出Excel报表",
                        Category = "生产管理",
                        Tags = "生产计划,查询,进度,甘特图",
                        Priority = 4,
                        IsEnabled = true,
                        MatchThreshold = 0.7m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "库存管理",
                        Question = "如何查看库存？库存不足怎么处理？",
                        Answer = "库存查询方法：\n1. 进入\"库存管理\"模块\n2. 选择\"库存查询\"功能\n3. 可按仓库、物料、时间等条件查询\n4. 支持实时库存和历史库存查询\n\n库存不足处理：\n- 系统会自动预警\n- 可设置安全库存阈值\n- 支持自动采购建议\n- 可查看供应商信息快速补货",
                        Category = "库存管理",
                        Tags = "库存,查询,不足,预警,采购",
                        Priority = 3,
                        IsEnabled = true,
                        MatchThreshold = 0.7m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "质量检验",
                        Question = "如何进行质量检验？质量标准在哪里查看？",
                        Answer = "质量检验流程：\n1. 进入\"质量管理\"模块\n2. 选择对应的检验项目\n3. 按照检验标准执行检验\n4. 录入检验结果\n5. 系统自动判定合格/不合格\n\n质量标准查看：\n- \"质量标准\"菜单查看所有标准\n- 支持按产品、工序查询\n- 可下载标准文档\n- 支持标准版本管理",
                        Category = "质量管理",
                        Tags = "质量,检验,标准,合格,不合格",
                        Priority = 3,
                        IsEnabled = true,
                        MatchThreshold = 0.7m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "系统故障处理",
                        Question = "系统出现错误怎么办？页面打不开如何处理？",
                        Answer = "系统故障处理步骤：\n1. 刷新页面重试\n2. 清除浏览器缓存\n3. 检查网络连接\n4. 尝试重新登录\n5. 更换浏览器测试\n\n如果问题仍然存在：\n- 记录错误信息和操作步骤\n- 截图保存错误页面\n- 联系技术支持：400-xxx-xxxx\n- 发送邮件到：support@company.com",
                        Category = "技术支持",
                        Tags = "故障,错误,页面,浏览器,技术支持",
                        Priority = 2,
                        IsEnabled = true,
                        MatchThreshold = 0.6m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIKnowledgeBase
                    {
                        KnowledgeId = (int)YitIdHelper.NextId(),
                        Title = "智能数据查询功能说明",
                        Question = "数据查询,系统数据,查询功能,智能查询,怎么查数据",
                        Answer = "🤖 我可以帮您查询各种系统数据！\n\n📋 工单查询示例：\n- \"查询工单信息\"\n- \"工单WO001的详情\"\n- \"今天有哪些工单\"\n\n📦 物料查询示例：\n- \"查询物料信息\"\n- \"物料M001的详情\"\n- \"所有产品物料\"\n\n🏢 客户查询示例：\n- \"查询客户信息\"\n- \"客户ABC公司的资料\"\n- \"所有客户列表\"\n\n🏗️ 仓库查询示例：\n- \"查询仓库状态\"\n- \"仓库库存情况\"\n- \"1号仓库信息\"\n\n🏭 生产线查询示例：\n- \"查询生产线信息\"\n- \"1号生产线状态\"\n- \"所有生产线\"\n\n📊 统计查询示例：\n- \"系统统计数据\"\n- \"数据汇总\"\n- \"系统概览\"\n\n💡 直接告诉我您想查什么，我会为您找到相关数据！",
                        Category = "系统功能",
                        Tags = "数据查询,智能查询,系统功能,帮助",
                        Priority = 5,
                        IsEnabled = true,
                        MatchThreshold = 0.7m,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    }
                };

                // 创建配置示例数据
                var configList = new List<AIServiceConfig>
                {
                    new AIServiceConfig
                    {
                        ConfigId = (int)YitIdHelper.NextId(),
                        ConfigKey = "ai_service_enabled",
                        ConfigValue = "true",
                        Description = "是否启用AI客服功能",
                        ConfigGroup = "ai_service",
                        IsEnabled = true,
                        Sort = 1,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIServiceConfig
                    {
                        ConfigId = (int)YitIdHelper.NextId(),
                        ConfigKey = "ai_response_timeout",
                        ConfigValue = "30000",
                        Description = "AI响应超时时间（毫秒）",
                        ConfigGroup = "ai_service",
                        IsEnabled = true,
                        Sort = 2,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIServiceConfig
                    {
                        ConfigId = (int)YitIdHelper.NextId(),
                        ConfigKey = "knowledge_match_threshold",
                        ConfigValue = "0.7",
                        Description = "知识库匹配度阈值",
                        ConfigGroup = "ai_service",
                        IsEnabled = true,
                        Sort = 3,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIServiceConfig
                    {
                        ConfigId = (int)YitIdHelper.NextId(),
                        ConfigKey = "max_conversation_history",
                        ConfigValue = "100",
                        Description = "最大对话历史记录数",
                        ConfigGroup = "ai_service",
                        IsEnabled = true,
                        Sort = 4,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    },
                    new AIServiceConfig
                    {
                        ConfigId = (int)YitIdHelper.NextId(),
                        ConfigKey = "welcome_message",
                        ConfigValue = "您好！我是AI智能客服，很高兴为您服务。请问有什么可以帮助您的吗？",
                        Description = "AI客服欢迎消息",
                        ConfigGroup = "ai_service",
                        IsEnabled = true,
                        Sort = 5,
                        CreateTime = DateTime.Now,
                        CreateName = "系统初始化",
                        isDel = false
                    }
                };

                // 添加到数据库
                await context.AIKnowledgeBases.AddRangeAsync(knowledgeList);
                await context.AIServiceConfigs.AddRangeAsync(configList);
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // 记录日志但不抛出异常，避免影响系统启动
                Console.WriteLine($"初始化AI客服示例数据失败: {ex.Message}");
            }
        }
    }
}
